﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using Entity;
using System.Threading;

namespace Project_V1
{
    public partial class FrmParamRivetSet : Form
    {

        private FastenerService objFastenerService = new FastenerService();
        private Fastener objFastener;//DGV初始加载和选择非当前行触发RowEnter事件,创建objFastener对象,刷新数据显示

        private LayerStackService objLayerStackService = new LayerStackService();
        private LayerStack objLayerStack;

        private SettingsService objSettingsService = new SettingsService();
        private Settings objSettings;//DGV初始加载和选择非当前行触发RowEnter事件,创建objSettings对象,刷新数据显示

        public FrmParamRivetSet()
        {
            InitializeComponent();
            this.Load += FrmParamRivetSet_Load;
        }

        private void UpdateText_Insert(Settings objSettings)//TextBox赋值并置为不可编辑
        {



           // this.txt_TechnicsNumber.Text = objSettings.TechnicsNumber.ToString();
            this.txt_MultistageId.Text = objSettings.MultistageId.ToString();
            this.txt_SettingsName.Text = objSettings.SettingsName;
            this.txt_SettingsId.Text = objSettings.SettingsId.ToString();
            this.txt_TargetVelo1.Text = objSettings.TargetVelo1.ToString();
            this.txt_TargetVelo2.Text = objSettings.TargetVelo2.ToString();
            this.txt_TargetVelo3.Text = objSettings.TargetVelo3.ToString();
            this.txt_TargetVelo4.Text = objSettings.TargetVelo4.ToString();
            this.txt_TargetVelo5.Text = objSettings.TargetVelo5.ToString();
            this.txt_RotationalSpeed1.Text = objSettings.RotationalSpeed1.ToString();
            this.txt_RotationalSpeed2.Text = objSettings.RotationalSpeed2.ToString();
            this.txt_RotationalSpeed3.Text = objSettings.RotationalSpeed3.ToString();
            this.txt_RotationalSpeed4.Text = objSettings.RotationalSpeed4.ToString();
            this.txt_RotationalSpeed5.Text = objSettings.RotationalSpeed5.ToString();
            this.txt_TargetPos1.Text = objSettings.TargetPos1.ToString();
            this.txt_TargetPos2.Text = objSettings.TargetPos2.ToString();
            this.txt_TargetPos3.Text = objSettings.TargetPos3.ToString();
            this.txt_TargetPos4.Text = objSettings.TargetPos4.ToString();
            this.txt_TargetPos5.Text = objSettings.TargetPos5.ToString();



            this.txt_TargetDownforce1.Text = objSettings.TargetDownforce1.ToString();
            this.txt_MaxFeedingTime.Text = objSettings.MaxFeedingTime.ToString();
            this.txt_MaxPrePressingTime.Text = objSettings.MaxPrePressingTime.ToString();
            this.txt_MaxRivetingTime.Text = objSettings.MaxRivetingTime.ToString();
            this.txt_UPLimitPos.Text = objSettings.UPLimitPos.ToString();
            this.txt_StopTorque.Text = objSettings.StopTorque.ToString();
            this.txt_Constant_Downforce1.Text = objSettings.Constant_Downforce1.ToString();
            this.txt_PressTime.Text = objSettings.PressTime.ToString();
            this.txt_TargetVelo6.Text = objSettings.TargetVelo6.ToString();
            this.txt_TargetPos6.Text = objSettings.TargetPos6.ToString();
            this.txt_FeedPos1.Text = objSettings.FeedPos1.ToString();
            this.txt_FeedVelo1.Text = objSettings.FeedVelo1.ToString();
            this.txt_FSR_FeedPos1.Text = objSettings.FSR_FeedPos1.ToString();
            this.txt_FeedVelo2.Text = objSettings.FeedVelo2.ToString();
            this.txt_FSR_ContactPos1.Text = objSettings.FSR_ContactPos1.ToString();
            this.txt_UpperPos1.Text = objSettings.UpperPos1.ToString();
            this.txt_LowerPos1.Text = objSettings.LowerPos1.ToString();
            this.txt_StopPressure1.Text = objSettings.StopPressure1.ToString();
            this.txt_Pre_Downforce1.Text = objSettings.Pre_Downforce1.ToString();
            this.txt_PrePressureSetValue.Text = objSettings.PrePressureSetValue.ToString();
            this.txt_FastenerName.Text = objSettings.FastenerName.ToString();
            //this.txt_FastenerId.Text = objSettings.FastenerId.ToString().PadLeft(2,'0');
            this.txt_Para_D.Text = objSettings.Para_D.ToString();
            this.txt_Para_P.Text = objSettings.Para_P.ToString();
            this.txt_Para_H.Text = objSettings.Para_H.ToString();
            this.txt_Para_L.Text = objSettings.Para_L.ToString();
            this.txt_LayerStackName.Text = objSettings.Material1.ToString().Trim() + "_" + objSettings.Thickness1.ToString().Trim() + "+" + objSettings.Material2.ToString().Trim() + "_" + objSettings.Thickness2.ToString().Trim();// objSettings.LayerStackName.ToString();
            //this.txt_LayerStackId.Text = objSettings.LayerStackId.ToString();
            this.txt_Material1.Text = objSettings.Material1.ToString();
            this.txt_Thickness1.Text = objSettings.Thickness1.ToString();
            this.txt_Material2.Text = objSettings.Material2.ToString();
            this.txt_Thickness2.Text = objSettings.Thickness2.ToString();





           //CommonMethods.FastenerList = objFastenerService.GetAll();
           //CommonMethods.LayerStackList = objLayerStackService.GetAll();
           // this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();



            //this.txt_Bias1.Text = objSettings.Bias1.ToString();
            //this.txt_TargetDownforce1.Text = objSettings.TargetDownforce1.ToString();
            //this.txt_TargetTorque1.Text = objSettings.TargetTorque1.ToString();
            //this.txt_t1.Text = objSettings.T1.ToString();
            //this.txt_FeedVelo1.Text = objSettings.FeedVelo1.ToString();
            //this.txt_FeedVelo2.Text = objSettings.FeedVelo2.ToString();

            this.cbo_Fastener.DataSource = null;
            this.cbo_Fastener.DisplayMember = "FastenerName";
            this.cbo_Fastener.ValueMember = "FastenerName";
            this.cbo_Fastener.DataSource = CommonMethods.FastenerList;

            this.cbo_LayerStack.DataSource = null;
            this.cbo_LayerStack.DisplayMember = "LayerStackName";
            this.cbo_LayerStack.ValueMember = "LayerStackName";
            this.cbo_LayerStack.DataSource = CommonMethods.LayerStackList;

            this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();
            this.cbo_LayerStack.SelectedValue = objSettings.Material1 + "_" + objSettings.Thickness1 + "+" + objSettings.Material2 + "_" + objSettings.Thickness2;
            this.cbo_Fastener.Enabled = false;
            this.cbo_LayerStack.Enabled = false;
            this.txt_SettingsName.Enabled = false;
            this.txt_SettingsId.Enabled = false;

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control item in page.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Enabled = false;
                    }
                }
            }
        }
        private void FrmParamRivetSet_Load(object sender, EventArgs e)
        {
            this.cbo_Fastener.DropDownStyle = ComboBoxStyle.DropDownList;//下拉列表不可编辑
            this.cbo_Fastener.DataSource = CommonMethods.FastenerList;
            this.cbo_Fastener.DisplayMember = "FastenerName";
            this.cbo_Fastener.ValueMember = "FastenerName";
            this.cbo_LayerStack.DropDownStyle = ComboBoxStyle.DropDownList;//下拉列表不可编辑
            this.cbo_LayerStack.DataSource = CommonMethods.LayerStackList;
            this.cbo_LayerStack.DisplayMember = "LayerStackName";
            this.cbo_LayerStack.ValueMember = "LayerStackName";

            //CommonMethods.SettingsList = objSettingsService.GetAll();//已经在主界面读取过集合
            this.dgv_data.AutoGenerateColumns = false;//只显示自定义的列
            this.dgv_data.DataSource = CommonMethods.SettingsList;

            foreach (Control item in this.MainPanel.Controls)
            {
                if (item is Button)
                {
                    item.Visible = false;
                }
            }
            this.txt_SettingsName.Enabled = false;
            this.txt_SettingsId.Enabled = false;
            this.cbo_Fastener.Enabled = false;
            this.cbo_LayerStack.Enabled = false;
        }

        #region 新增行,需要点击确定\取消
        private void btn_New_Click(object sender, EventArgs e)
        {
            //this.groupBox1.Text = "新增界面";

            this.cbo_Fastener.Enabled = true;
            this.cbo_LayerStack.Enabled = true;
            this.txt_SettingsName.Enabled = true;
            this.txt_SettingsId.Enabled = true;
            this.cbo_Fastener.SelectedIndex = -1;
            this.cbo_LayerStack.SelectedIndex = -1;
            this.txt_SettingsId.Text = "";
            this.txt_SettingsName.Text = "";

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control item in page.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Enabled = true;
                        item.Text = "";
                    }
                }
            }

            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            this.btn_SaveNew.Visible = true;
            this.btn_Cancel.Visible = true;
            this.btn_SaveEdit.Visible = false;
        }
        #endregion

        #region 编辑行,需要点击确定\取消,可继续细化文本改变时才可保存
        private void btn_Edit_Click(object sender, EventArgs e)
        {
            //this.groupBox1.Text = "编辑界面";
            if (this.dgv_data.RowCount == 0)
            {
                MessageBox.Show("没有要编辑的行！", "提示信息");
                return;
            }
            if (this.dgv_data.CurrentRow == null)
            {
                MessageBox.Show("请选中要编辑的行！", "提示信息");
                return;
            }

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control item in page.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Enabled = true;
                    }
                }
            }
            this.txt_SettingsName.Enabled = true;
            this.txt_SettingsId.Enabled = true;
            this.cbo_Fastener.Enabled = true;
            this.cbo_LayerStack.Enabled = true;

            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            this.btn_SaveEdit.Visible = true;
            this.btn_Cancel.Visible = true;
            this.btn_SaveNew.Visible = false;

            this.btn_PressConfirmed.Enabled = true;
            this.btn_StopTorqueConfirmed.Enabled = true;
        }
        #endregion

        #region 删除行,弹出对话框确认是否删除
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (this.dgv_data.RowCount == 0)
            {
                MessageBox.Show("没有要删除的行！", "提示信息");
                return;
            }
            if (this.dgv_data.CurrentRow == null)
            {
                MessageBox.Show("请选中要删除的行！", "提示信息");
                return;
            }
            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            //弹出对话框,确认是否删除
            string settingsName = this.dgv_data.CurrentRow.Cells["SettingsName"].Value.ToString();
            DialogResult result = MessageBox.Show("确认要删除紧固件  [" + settingsName + "]  吗？", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.Cancel)
            {
                this.dgv_data.Enabled = true;
                this.btn_Delete.Enabled = true;
                this.btn_New.Enabled = true;
                this.btn_Edit.Enabled = true;
                return;
            }

            //获取编号并删除
            string settingsId = this.dgv_data.CurrentRow.Cells["SettingsId"].Value.ToString();
            try
            {
                if (objSettingsService.DeleteSettingsById(settingsId) == 1)
                {
                    //btnQuery_Click(null, null);//同步刷新修改的信息
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息");
            }

            UpdateDGV();

            this.dgv_data.Enabled = true;
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;
        }
        #endregion

        #region 取消按钮
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            UpdateText();
            this.dgv_data.Enabled = true;//点击取消
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }
        #endregion

        //弹出MessageBox(更新成功),编辑需封装EditId
        private void btn_SaveEdit_Click(object sender, EventArgs e)
        {
            #region 数据验证,还需要继续添加,comboBox的选择判断

            //if (this.cbo_Fastener.SelectedIndex == -1)
            //{
            //    MessageBox.Show("请选择紧固件 ! ", "提示信息");
            //    this.cbo_Fastener.Focus();
            //    return;
            //}
            //if (this.cbo_LayerStack.SelectedIndex == -1)
            //{
            //    MessageBox.Show("请选择板材组合 ! ", "提示信息");
            //    this.cbo_LayerStack.Focus();
            //    return;
            //}

            if (this.txt_SettingsName.Text.Trim().Length == 0)
            {
                MessageBox.Show("工艺规范名称不能为空 ! ", "提示信息");
                this.txt_SettingsName.Focus();
                return;
            }
            if (this.txt_SettingsId.Text.Trim().Length == 0)
            {
                MessageBox.Show("工艺规范编号不能为空 ! ", "提示信息");
                this.txt_SettingsId.Focus();
                return;
            }
            if (this.txt_TargetVelo1.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
                this.txt_TargetVelo1.Focus();
                return;
            }
            if (this.txt_RotationalSpeed1.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
                this.txt_RotationalSpeed1.Focus();
                return;
            }
            if (this.txt_TargetPos1.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
                this.txt_TargetPos1.Focus();
                return;
            }
            //if (this.txt_Bias1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Bias1.Focus();
            //    return;
            //}
            //if (this.txt_TargetDownforce1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_TargetDownforce1.Focus();
            //    return;
            //}
            //if (this.txt_TargetTorque1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_TargetTorque1.Focus();
            //    return;
            //}
            //if (this.txt_t1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_t1.Focus();
            //    return;
            //}
            //if (this.txt_FeedVelo1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_FeedVelo1.Focus();
            //    return;
            //}
            //if (this.txt_FeedVelo2.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_FeedVelo2.Focus();
            //    return;
            //}

            //获取选中行的编号
            //this.dgv_data.SelectedRows[0].Cells["SettingsId"].Value
            int a = Convert.ToInt32(this.dgv_data.CurrentRow.Cells["SettingsId"].Value);

            //验证编号是否在数据库内已经存在,并且不等于选中行编号则退出
            if (objSettingsService.IsIdNoExisted(this.txt_SettingsId.Text.Trim()))
            {
                if (a != Convert.ToInt32(this.txt_SettingsId.Text.Trim()))
                {
                    MessageBox.Show("工艺规范编号不能和现有编号重复！", "验证提示");
                    this.txt_SettingsId.Focus();
                    this.txt_SettingsId.SelectAll();
                    return;
                }
            }
            #endregion

            #region 封装工艺规范对象
            Settings objSettings = new Settings()
            {
                MultistageId= Convert.ToInt32(this.txt_MultistageId.Text.Trim()),
                SettingsName = this.txt_SettingsName.Text.Trim(),
                SettingsId = Convert.ToInt32(this.txt_SettingsId.Text.Trim()),
                //FastenerId = Convert.ToString(this.txt_FastenerId.Text.Trim()),//
                //LayerStackId = Convert.ToInt32(this.cbo_LayerStack.SelectedValue),//
                TargetVelo1 = Convert.ToSingle(this.txt_TargetVelo1.Text.Trim()),
                TargetVelo2 = Convert.ToSingle(this.txt_TargetVelo2.Text.Trim()),
                TargetVelo3 = Convert.ToSingle(this.txt_TargetVelo3.Text.Trim()),
                TargetVelo4 = Convert.ToSingle(this.txt_TargetVelo4.Text.Trim()),
                TargetVelo5 = Convert.ToSingle(this.txt_TargetVelo5.Text.Trim()),
                RotationalSpeed1 = Convert.ToSingle(this.txt_RotationalSpeed1.Text.Trim()),
                RotationalSpeed2 = Convert.ToSingle(this.txt_RotationalSpeed2.Text.Trim()),
                RotationalSpeed3 = Convert.ToSingle(this.txt_RotationalSpeed3.Text.Trim()),
                RotationalSpeed4 = Convert.ToSingle(this.txt_RotationalSpeed4.Text.Trim()),
                RotationalSpeed5 = Convert.ToSingle(this.txt_RotationalSpeed5.Text.Trim()),
                TargetPos1 = Convert.ToSingle(this.txt_TargetPos1.Text.Trim()),
                TargetPos2 = Convert.ToSingle(this.txt_TargetPos2.Text.Trim()),
                TargetPos3 = Convert.ToSingle(this.txt_TargetPos3.Text.Trim()),
                TargetPos4 = Convert.ToSingle(this.txt_TargetPos4.Text.Trim()),
                TargetPos5 = Convert.ToSingle(this.txt_TargetPos5.Text.Trim()),

                TargetDownforce1 = Convert.ToSingle(this.txt_TargetDownforce1.Text.Trim()),
                MaxFeedingTime = Convert.ToInt32(this.txt_MaxFeedingTime.Text.Trim()),
                MaxPrePressingTime = Convert.ToInt32(this.txt_MaxPrePressingTime.Text.Trim()),
                MaxRivetingTime = Convert.ToInt32(this.txt_MaxRivetingTime.Text.Trim()),
                UPLimitPos = Convert.ToSingle(this.txt_UPLimitPos.Text.Trim()),
                StopTorque = Convert.ToSingle(this.txt_StopTorque.Text.Trim()),
                Constant_Downforce1 = Convert.ToSingle(this.txt_Constant_Downforce1.Text.Trim()),
                PressTime = Convert.ToInt32(this.txt_PressTime.Text.Trim()),
                TargetVelo6 = Convert.ToSingle(this.txt_TargetVelo6.Text.Trim()),
                TargetPos6 = Convert.ToSingle(this.txt_TargetPos6.Text.Trim()),
                FeedPos1 = Convert.ToSingle(this.txt_FeedPos1.Text.Trim()),
                FeedVelo1 = Convert.ToSingle(this.txt_FeedVelo1.Text.Trim()),
                FSR_FeedPos1 = Convert.ToSingle(this.txt_FSR_FeedPos1.Text.Trim()),
                FeedVelo2 = Convert.ToSingle(this.txt_FeedVelo2.Text.Trim()),
                FSR_ContactPos1 = Convert.ToSingle(this.txt_FSR_ContactPos1.Text.Trim()),
                UpperPos1 = Convert.ToSingle(this.txt_UpperPos1.Text.Trim()),
                LowerPos1 = Convert.ToSingle(this.txt_LowerPos1.Text.Trim()),
                StopPressure1 = Convert.ToSingle(this.txt_StopPressure1.Text.Trim()),
                Pre_Downforce1 = Convert.ToSingle(this.txt_Pre_Downforce1.Text.Trim()),
                PrePressureSetValue = Convert.ToSingle(this.txt_PrePressureSetValue.Text.Trim()),
                FastenerName = Convert.ToString(this.txt_FastenerName.Text.Trim()),
                Para_D = Convert.ToString(this.txt_Para_D.Text.Trim()),
                Para_P = Convert.ToString(this.txt_Para_P.Text.Trim()),
                Para_H = Convert.ToString(this.txt_Para_H.Text.Trim()),
                Para_L = Convert.ToString(this.txt_Para_L.Text.Trim()),
                LayerStackName = this.txt_Material1.Text.Trim() + "_" + this.txt_Material2.Text.Trim() + "+" + this.txt_Thickness1.Text.Trim() + "_" + this.txt_Thickness2.Text.Trim(), //Convert.ToString(this.txt_LayerStackName.Text.Trim()),
                Material1 = Convert.ToString(this.txt_Material1.Text.Trim()),
                Thickness1 = Convert.ToString(this.txt_Thickness1.Text.Trim()),
                Material2 = Convert.ToString(this.txt_Material2.Text.Trim()),
                Thickness2 = Convert.ToString(this.txt_Thickness2.Text.Trim()),

                PressConfirmed = (this.btn_PressConfirmed.BackColor == Color.FromArgb(60, 179, 113) ? true : false),
                StopTorqueConfirmed = (this.btn_StopTorqueConfirmed.BackColor == Color.FromArgb(60, 179, 113) ? true : false),

                EidtId = a
            };
            #endregion
            //return;

            #region 封装紧固件对象
            Fastener objFastener = new Fastener()
            {
                FastenerName = this.txt_FastenerName.Text.Trim(),// + txt_FastenerId.Text.Trim(),
                //FastenerId = Convert.ToInt32(this.txt_FastenerId.Text.Trim()),
                Para_D = Convert.ToSingle(this.txt_Para_D.Text.Trim()),
                Para_P = Convert.ToSingle(this.txt_Para_P.Text.Trim()),
                Para_H = Convert.ToSingle(this.txt_Para_H.Text.Trim()),
                Para_L = Convert.ToSingle(this.txt_Para_L.Text.Trim()), 
            };

            #endregion




            if (objFastenerService.IsFastenerNameExisted(objFastener.FastenerName))  //1存在
            {

                #region 调用后台数据访问方法,将更新后的对象信息存入数据库
                try
                {
                    if (objFastenerService.ModifyNewFastener(objFastener) == 1)
                    {
                        //MessageBox.Show("紧固件参数更新成功", "提示信息");
                    }
                    else
                    {
                        //MessageBox.Show("更新失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion

            }
            else
            {

                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objFastenerService.AddNewFastener(objFastener) == 1)
                    {
                       // MessageBox.Show("新紧固件添加成功", "提示信息");
                    }
                    else
                    {
                        //MessageBox.Show("添加失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }










            #region 封装板材组合对象
            LayerStack objLayerStack = new LayerStack()
            {
                LayerStackName = this.txt_Material1.Text.Trim() + "_" + this.txt_Thickness1.Text.Trim() + "+" + this.txt_Material2.Text.Trim() + "_" + this.txt_Thickness2.Text.Trim(),//this.txt_LayerStackName.Text.Trim(),
                //LayerStackId = Convert.ToInt32(this.txt_LayerStackId.Text.Trim()),
                Material1 = Convert.ToString(this.txt_Material1.Text.Trim()),
                Material2 = Convert.ToString(this.txt_Material2.Text.Trim()),
                Thickness1 = Convert.ToSingle(this.txt_Thickness1.Text.Trim()),
                Thickness2 = Convert.ToSingle(this.txt_Thickness2.Text.Trim())
            };
            #endregion

            //验证编号是否在数据库内已经存在
            if (objLayerStackService.IsNewLayerStackNameExisted(objLayerStack.LayerStackName))
            {

                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objLayerStackService.ModifyNewLayerStackName(objLayerStack) == 1)
                    {
                      //  MessageBox.Show("板材组合参数更新成功", "提示信息");
                    }
                    else
                    {
                       // MessageBox.Show("更新失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }
            else
            {
                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objLayerStackService.AddNewLayerStackName(objLayerStack) == 1)
                    {
                       // MessageBox.Show("新板材组合添加成功", "提示信息");
                    }
                    else
                    {
                       // MessageBox.Show("添加失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }


























            #region 调用后台数据访问方法,将对象信息存入数据库
            try
            {
                if (objSettingsService.ModifySettings(objSettings) == 1)
                {
                    MessageBox.Show("工艺规范参数更新成功", "提示信息");
                }
                else
                {
                    MessageBox.Show("更新失败", "提示信息");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion

            UpdateDGV();

            CommonMethods.FastenerList = objFastenerService.GetAll();
            CommonMethods.LayerStackList = objLayerStackService.GetAll();
            this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();

            for (int i = 0; i < dgv_data.Rows.Count; i++)
            {
                dgv_data.Rows[i].Selected = false;
            }

            for (int i = 0; i < dgv_data.Rows.Count; i++)
            {
                if (dgv_data.Rows[i].Cells[0].Value.ToString() == objSettings.SettingsId.ToString())
                {
                    dgv_data.Rows[i].Selected = true;
                    UpdateText_Insert(objSettings);
                }
            }
      



            this.dgv_data.Enabled = true;//点击确定,编辑完成
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }

        private void btn_SaveNew_Click(object sender, EventArgs e)
        {
            #region 数据验证,还需要继续添加,comboBox的选择判断

            //if (this.cbo_Fastener.SelectedIndex == -1)
            //{
            //    MessageBox.Show("请选择紧固件 ! ", "提示信息");
            //    this.cbo_Fastener.Focus();
            //    return;
            //}
            //if (this.cbo_LayerStack.SelectedIndex == -1)
            //{
            //    MessageBox.Show("请选择板材组合 ! ", "提示信息");
            //    this.cbo_LayerStack.Focus();
            //    return;
            //}

            if (this.txt_SettingsName.Text.Trim().Length == 0)
            {
                MessageBox.Show("工艺规范名称不能为空 ! ", "提示信息");
                this.txt_SettingsName.Focus();
                return;
            }
            if (this.txt_SettingsId.Text.Trim().Length == 0)
            {
                MessageBox.Show("工艺规范编号不能为空 ! ", "提示信息");
                this.txt_SettingsId.Focus();
                return;
            }
            if (this.txt_TargetVelo1.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
                this.txt_TargetVelo1.Focus();
                return;
            }
            if (this.txt_RotationalSpeed1.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
                this.txt_RotationalSpeed1.Focus();
                return;
            }
            if (this.txt_TargetPos1.Text.Trim().Length == 0)
            {
                MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
                this.txt_TargetPos1.Focus();
                return;
            }
            //if (this.txt_Bias1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Bias1.Focus();
            //    return;
            //}
            //if (this.txt_TargetDownforce1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_TargetDownforce1.Focus();
            //    return;
            //}
            //if (this.txt_TargetTorque1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_TargetTorque1.Focus();
            //    return;
            //}
            //if (this.txt_t1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_t1.Focus();
            //    return;
            //}
            //if (this.txt_FeedVelo1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_FeedVelo1.Focus();
            //    return;
            //}
            //if (this.txt_FeedVelo2.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_FeedVelo2.Focus();
            //    return;
            //}

            //验证编号是否在数据库内已经存在
            if (objSettingsService.IsIdNoExisted(this.txt_SettingsId.Text.Trim()))
            {
                MessageBox.Show("工艺规范编号不能和现有编号重复！", "验证提示");
                this.txt_SettingsId.Focus();
                this.txt_SettingsId.SelectAll();
                return;
            }
            #endregion

            #region 封装工艺规范对象
            Settings objSettings = new Settings()
            {
                MultistageId = Convert.ToInt32(this.txt_MultistageId.Text.Trim()),
                SettingsName = this.txt_SettingsName.Text.Trim(),
                SettingsId = Convert.ToInt32(this.txt_SettingsId.Text.Trim()),
                //FastenerId = Convert.ToString(this.txt_FastenerId.Text.Trim()),//
                //FastenerId = Convert.ToString(this.cbo_Fastener.SelectedValue),
                LayerStackId = Convert.ToInt32(this.cbo_LayerStack.SelectedValue),
                TargetVelo1 = Convert.ToSingle(this.txt_TargetVelo1.Text.Trim()),
                TargetVelo2 = Convert.ToSingle(this.txt_TargetVelo2.Text.Trim()),
                TargetVelo3 = Convert.ToSingle(this.txt_TargetVelo3.Text.Trim()),
                TargetVelo4 = Convert.ToSingle(this.txt_TargetVelo4.Text.Trim()),
                TargetVelo5 = Convert.ToSingle(this.txt_TargetVelo5.Text.Trim()),
                RotationalSpeed1 = Convert.ToSingle(this.txt_RotationalSpeed1.Text.Trim()),
                RotationalSpeed2 = Convert.ToSingle(this.txt_RotationalSpeed2.Text.Trim()),
                RotationalSpeed3 = Convert.ToSingle(this.txt_RotationalSpeed3.Text.Trim()),
                RotationalSpeed4 = Convert.ToSingle(this.txt_RotationalSpeed4.Text.Trim()),
                RotationalSpeed5 = Convert.ToSingle(this.txt_RotationalSpeed5.Text.Trim()),
                TargetPos1 = Convert.ToSingle(this.txt_TargetPos1.Text.Trim()),
                TargetPos2 = Convert.ToSingle(this.txt_TargetPos2.Text.Trim()),
                TargetPos3 = Convert.ToSingle(this.txt_TargetPos3.Text.Trim()),
                TargetPos4 = Convert.ToSingle(this.txt_TargetPos4.Text.Trim()),
                TargetPos5 = Convert.ToSingle(this.txt_TargetPos5.Text.Trim()),


                TargetDownforce1 = Convert.ToSingle(this.txt_TargetDownforce1.Text.Trim()),
                MaxFeedingTime = Convert.ToInt32(this.txt_MaxFeedingTime.Text.Trim()),
                MaxPrePressingTime = Convert.ToInt32(this.txt_MaxPrePressingTime.Text.Trim()),
                MaxRivetingTime = Convert.ToInt32(this.txt_MaxRivetingTime.Text.Trim()),
                UPLimitPos = Convert.ToSingle(this.txt_UPLimitPos.Text.Trim()),
                StopTorque = Convert.ToSingle(this.txt_StopTorque.Text.Trim()),
                Constant_Downforce1 = Convert.ToSingle(this.txt_Constant_Downforce1.Text.Trim()),
                PressTime = Convert.ToInt32(this.txt_PressTime.Text.Trim()),
                TargetVelo6 = Convert.ToSingle(this.txt_TargetVelo6.Text.Trim()),
                TargetPos6 = Convert.ToSingle(this.txt_TargetPos6.Text.Trim()),
                FeedPos1 = Convert.ToSingle(this.txt_FeedPos1.Text.Trim()),
                FeedVelo1 = Convert.ToSingle(this.txt_FeedVelo1.Text.Trim()),
                FSR_FeedPos1 = Convert.ToSingle(this.txt_FSR_FeedPos1.Text.Trim()),
                FeedVelo2 = Convert.ToSingle(this.txt_FeedVelo2.Text.Trim()),
                FSR_ContactPos1 = Convert.ToSingle(this.txt_FSR_ContactPos1.Text.Trim()),
                UpperPos1 = Convert.ToSingle(this.txt_UpperPos1.Text.Trim()),
                LowerPos1 = Convert.ToSingle(this.txt_LowerPos1.Text.Trim()),
                StopPressure1 = Convert.ToSingle(this.txt_StopPressure1.Text.Trim()),
                Pre_Downforce1 = Convert.ToSingle(this.txt_Pre_Downforce1.Text.Trim()),
                PrePressureSetValue = Convert.ToSingle(this.txt_PrePressureSetValue.Text.Trim()),
                FastenerName = Convert.ToString(this.txt_FastenerName.Text.Trim()),
                Para_D = Convert.ToString(this.txt_Para_D.Text.Trim()),
                Para_P = Convert.ToString(this.txt_Para_P.Text.Trim()),
                Para_H = Convert.ToString(this.txt_Para_H.Text.Trim()),
                Para_L = Convert.ToString(this.txt_Para_L.Text.Trim()),
                LayerStackName = this.txt_Material1.Text.Trim() + "_" + this.txt_Material2.Text.Trim() + "+" + this.txt_Thickness1.Text.Trim() + "_" + this.txt_Thickness2.Text.Trim(),//Convert.ToString(this.txt_LayerStackName.Text.Trim()),
                Material1 = Convert.ToString(this.txt_Material1.Text.Trim()),
                Thickness1 = Convert.ToString(this.txt_Thickness1.Text.Trim()),
                Material2 = Convert.ToString(this.txt_Material2.Text.Trim()),
                Thickness2 = Convert.ToString(this.txt_Thickness2.Text.Trim()),



                PressConfirmed = (this.btn_PressConfirmed.BackColor == Color.FromArgb(60, 179, 113) ? true : false),//    this.btn_PressConfirmed.Enabled,
                StopTorqueConfirmed = (this.btn_StopTorqueConfirmed.BackColor == Color.FromArgb(60, 179, 113) ? true : false) //this.btn_PressConfirmed.Enabled
 
            };

            #endregion






            #region 封装紧固件对象
            Fastener objFastener = new Fastener()
            {
                FastenerName = this.txt_FastenerName.Text.Trim(),// + txt_FastenerId.Text.Trim(),
                //FastenerId = Convert.ToInt32(this.txt_FastenerId.Text.Trim()),
                Para_D = Convert.ToSingle(this.txt_Para_D.Text.Trim()),
                Para_P = Convert.ToSingle(this.txt_Para_P.Text.Trim()),
                Para_H = Convert.ToSingle(this.txt_Para_H.Text.Trim()),
                Para_L = Convert.ToSingle(this.txt_Para_L.Text.Trim()),
            }; 
            #endregion
             


            if (objFastenerService.IsFastenerNameExisted(this.txt_FastenerName.Text.Trim() ))  //1存在+ txt_FastenerId.Text.Trim()
            {

                #region 调用后台数据访问方法,将更新后的对象信息存入数据库
                try
                {
                    if (objFastenerService.ModifyNewFastener(objFastener) == 1)
                    {
                       // MessageBox.Show("紧固件参数更新成功", "提示信息");
                    }
                    else
                    {
                       // MessageBox.Show("更新失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion

            }
            else
            {

                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objFastenerService.AddNewFastener(objFastener) == 1)
                    {
                      // MessageBox.Show("新紧固件添加成功", "提示信息");
                    }
                    else
                    {
                       // MessageBox.Show("添加失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }







         


            #region 封装板材组合对象
            LayerStack objLayerStack = new LayerStack()
            {
                LayerStackName = this.txt_Material1.Text.Trim() + "_" + this.txt_Thickness1.Text.Trim() + "+" + this.txt_Material2.Text.Trim() + "_" + this.txt_Thickness2.Text.Trim(),//this.txt_LayerStackName.Text.Trim(),
                LayerStackId = Convert.ToInt32(this.txt_LayerStackId.Text.Trim()),
                Material1 = Convert.ToString(this.txt_Material1.Text.Trim()),
                Material2 = Convert.ToString(this.txt_Material2.Text.Trim()),
                Thickness1 = Convert.ToSingle(this.txt_Thickness1.Text.Trim()),
                Thickness2 = Convert.ToSingle(this.txt_Thickness2.Text.Trim())
            };
            #endregion

            txt_LayerStackName.Text = objLayerStack.LayerStackName;
            //验证编号是否在数据库内已经存在
            if (objLayerStackService.IsNewLayerStackNameExisted(objLayerStack.LayerStackName))
            {

                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objLayerStackService.ModifyNewLayerStackName(objLayerStack) == 1)
                    {
                       //MessageBox.Show("板材组合参数更新成功", "提示信息");
                    }
                    else
                    {
                       //MessageBox.Show("更新失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }
            else
            {
                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objLayerStackService.AddNewLayerStackName(objLayerStack) == 1)
                    {
                        //MessageBox.Show("新板材组合添加成功", "提示信息");
                    }
                    else
                    {
                       // MessageBox.Show("添加失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }






















            #region 调用后台数据访问方法,将对象信息存入数据库
            try
            {
                if (objSettingsService.AddSettings(objSettings) == 1)
                {
                    MessageBox.Show("新工艺规范添加成功", "提示信息");
                }
                else
                {
                    MessageBox.Show("添加失败", "提示信息");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion











            UpdateDGV();
            CommonMethods.FastenerList = objFastenerService.GetAll();
            CommonMethods.LayerStackList = objLayerStackService.GetAll();
            this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();

            for (int i = 0; i < dgv_data.Rows.Count; i++)
            {
                dgv_data.Rows[i].Selected = false;
            }

            for (int i = 0; i < dgv_data.Rows.Count; i++)
            {
                if (dgv_data.Rows[i].Cells[0].Value.ToString() == objSettings.SettingsId.ToString())
                {
                    dgv_data.Rows[i].Selected = true;
                    UpdateText_Insert(objSettings);
                }
            }

  
            this.dgv_data.Enabled = true;//点击确定,编辑完成
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }

        #region 被选中行RowEnter事件,得到被选中行的索引值,和SettingsList集合的索引一一对应
        private void dgv_data_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //DGV初始加载和选择非当前行触发RowEnter事件,该事件结束后CurrentRow属性才会被更新
            objSettings = new Settings();
            int i = e.RowIndex;//索引值按照由小到大,集合由数据库默认按照主键增序存入,索引一一对应
            objSettings = CommonMethods.SettingsList[i];
            //objSettings.EidtId = Convert.ToInt32(this.dgv_data.Rows[i].Cells["SettingsId"].Value);
            UpdateText();
        }
        #endregion

        #region UpdateDGV()从数据库读取信息存入集合,重新绑定刷新DataGridView
        private void UpdateDGV()//从数据库读取集合信息
        {
            CommonMethods.SettingsList = objSettingsService.GetAll();
            this.dgv_data.DataSource = null;
            this.dgv_data.DataSource = CommonMethods.SettingsList;
        }
        #endregion

        #region UpdateDGV_ByName()从数据库模糊查询读取信息存入集合,重新绑定刷新DataGridView
        private void UpdateDGV_ByName(string vname)//从数据库读取集合信息
        {
            CommonMethods.SettingsList = objSettingsService.GetSearch(vname);
            this.dgv_data.DataSource = null;
            this.dgv_data.DataSource = CommonMethods.SettingsList;
        }

        #endregion

        #region UpdateText()更新文本等显示
        /// <summary>
        /// 更新TextBox等显示
        /// </summary>
        private void UpdateText()//TextBox赋值并置为不可编辑
        {
            this.txt_MultistageId.Text = objSettings.MultistageId.ToString();
            this.txt_SettingsName.Text = objSettings.SettingsName;
            this.txt_SettingsId.Text = objSettings.SettingsId.ToString();
            this.txt_TargetVelo1.Text = objSettings.TargetVelo1.ToString();
            this.txt_TargetVelo2.Text = objSettings.TargetVelo2.ToString();
            this.txt_TargetVelo3.Text = objSettings.TargetVelo3.ToString();
            this.txt_TargetVelo4.Text = objSettings.TargetVelo4.ToString();
            this.txt_TargetVelo5.Text = objSettings.TargetVelo5.ToString();
            this.txt_RotationalSpeed1.Text = objSettings.RotationalSpeed1.ToString();
            this.txt_RotationalSpeed2.Text = objSettings.RotationalSpeed2.ToString();
            this.txt_RotationalSpeed3.Text = objSettings.RotationalSpeed3.ToString();
            this.txt_RotationalSpeed4.Text = objSettings.RotationalSpeed4.ToString();
            this.txt_RotationalSpeed5.Text = objSettings.RotationalSpeed5.ToString();
            this.txt_TargetPos1.Text = objSettings.TargetPos1.ToString();
            this.txt_TargetPos2.Text = objSettings.TargetPos2.ToString();
            this.txt_TargetPos3.Text = objSettings.TargetPos3.ToString();
            this.txt_TargetPos4.Text = objSettings.TargetPos4.ToString();
            this.txt_TargetPos5.Text = objSettings.TargetPos5.ToString();


            this.txt_TargetDownforce1.Text = objSettings.TargetDownforce1.ToString();
            this.txt_MaxFeedingTime.Text = objSettings.MaxFeedingTime.ToString();
            this.txt_MaxPrePressingTime.Text = objSettings.MaxPrePressingTime.ToString();
            this.txt_MaxRivetingTime.Text = objSettings.MaxRivetingTime.ToString();
            this.txt_UPLimitPos.Text = objSettings.UPLimitPos.ToString();
            this.txt_StopTorque.Text = objSettings.StopTorque.ToString();
            this.txt_Constant_Downforce1.Text = objSettings.Constant_Downforce1.ToString();
            this.txt_PressTime.Text = objSettings.PressTime.ToString();
            this.txt_TargetVelo6.Text = objSettings.TargetVelo6.ToString();
            this.txt_TargetPos6.Text = objSettings.TargetPos6.ToString();
            this.txt_FeedPos1.Text = objSettings.FeedPos1.ToString();
            this.txt_FeedVelo1.Text = objSettings.FeedVelo1.ToString();
            this.txt_FSR_FeedPos1.Text = objSettings.FSR_FeedPos1.ToString();
            this.txt_FeedVelo2.Text = objSettings.FeedVelo2.ToString();
            this.txt_FSR_ContactPos1.Text = objSettings.FSR_ContactPos1.ToString();
            this.txt_UpperPos1.Text = objSettings.UpperPos1.ToString();
            this.txt_LowerPos1.Text = objSettings.LowerPos1.ToString();
            this.txt_StopPressure1.Text = objSettings.StopPressure1.ToString();
            this.txt_Pre_Downforce1.Text = objSettings.Pre_Downforce1.ToString();
            this.txt_PrePressureSetValue.Text = objSettings.PrePressureSetValue.ToString();
            this.txt_FastenerName.Text = objSettings.FastenerName.ToString();
            //this.txt_FastenerId.Text = objSettings.FastenerId.ToString().PadLeft(2, '0');
            this.txt_Para_D.Text = objSettings.Para_D.ToString();
            this.txt_Para_P.Text = objSettings.Para_P.ToString();
            this.txt_Para_H.Text = objSettings.Para_H.ToString();
            this.txt_Para_L.Text = objSettings.Para_L.ToString();
            this.txt_LayerStackName.Text = objSettings.Material1.ToString().Trim() + "_" + objSettings.Thickness1.ToString().Trim() + "+" + objSettings.Material2.ToString().Trim() + "_" + objSettings.Thickness2.ToString().Trim();//    objSettings.LayerStackName.ToString();
            //this.txt_LayerStackId.Text = objSettings.LayerStackId.ToString();
            this.txt_Material1.Text = objSettings.Material1.ToString();
            this.txt_Thickness1.Text = objSettings.Thickness1.ToString();
            this.txt_Material2.Text = objSettings.Material2.ToString();
            this.txt_Thickness2.Text = objSettings.Thickness2.ToString();

            this.btn_PressConfirmed.Enabled = false;
            this.btn_StopTorqueConfirmed.Enabled = false;
            //if(this.btn_PressConfirmed.colo)
            if (objSettings.PressConfirmed == true)
                this.btn_PressConfirmed.BackColor = Color.FromArgb(60, 179, 113);
               else
                this.btn_PressConfirmed.BackColor = Color.FromArgb(255, 255, 255);
            //this.btn_PressConfirmed.Enabled = objSettings.PressConfirmed;

            if (objSettings.StopTorqueConfirmed == true)
                this.btn_StopTorqueConfirmed.BackColor = Color.FromArgb(60, 179, 113);
            else
                this.btn_StopTorqueConfirmed.BackColor = Color.FromArgb(255, 255, 255);

            //this.btn_StopTorqueConfirmed.Enabled = objSettings.StopTorqueConfirmed;
     



            //this.txt_Bias1.Text = objSettings.Bias1.ToString();
            //this.txt_TargetDownforce1.Text = objSettings.TargetDownforce1.ToString();
            //this.txt_TargetTorque1.Text = objSettings.TargetTorque1.ToString();
            //this.txt_t1.Text = objSettings.T1.ToString();
            //this.txt_FeedVelo1.Text = objSettings.FeedVelo1.ToString();
            //this.txt_FeedVelo2.Text = objSettings.FeedVelo2.ToString();

            this.cbo_Fastener.DataSource = null;
            this.cbo_Fastener.DisplayMember = "FastenerName";
            this.cbo_Fastener.ValueMember = "FastenerName";
            this.cbo_Fastener.DataSource = CommonMethods.FastenerList;

            this.cbo_LayerStack.DataSource = null;
            this.cbo_LayerStack.DisplayMember = "LayerStackName";
            this.cbo_LayerStack.ValueMember = "LayerStackName";
            this.cbo_LayerStack.DataSource = CommonMethods.LayerStackList;

            this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();
            this.cbo_LayerStack.SelectedValue = objSettings.Material1+"_"+ objSettings.Thickness1 + "+" + objSettings.Material2 + "_" + objSettings.Thickness2;
            this.cbo_Fastener.Enabled = false;
            this.cbo_LayerStack.Enabled = false;
            this.txt_SettingsName.Enabled = false;
            this.txt_SettingsId.Enabled = false;

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control item in page.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Enabled = false;
                    }
                }
            }
        }

        #endregion

        private void dgv_data_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.btn_Edit_Click(null, null);
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            UpdateDGV_ByName(txt_SearchName.Text);
        }



        public static void UpdateParameter(string VariableName, string val)
        {
            Beckhoff_PLC.IsWriting = true;
            if (VariableName.IndexOf("Main_Sys.Mach") != -1) VariableName = VariableName.TrimStart('.');
            else if (VariableName.IndexOf("I_") != -1) VariableName = VariableName.TrimStart('.');
            else if (VariableName.IndexOf("Main_Sys.JOGSetVelo") != -1) VariableName = VariableName.TrimStart('.');
            else VariableName = "_03_HMI" + VariableName;
            if (Beckhoff_PLC.WriteVariable(VariableName, float.Parse(val.Trim())))//将每个变量的变量名传来,在Tag中
            {
            }
            else
            {
                //MessageBox.Show("修改失败！", "修改提示");
            }
            Beckhoff_PLC.IsWriting = false;
        }


        public static void UpdateParameter_int(string VariableName, string val)
        {
            Beckhoff_PLC.IsWriting = true;
            if (VariableName.IndexOf("Main_Sys.Mach") != -1) VariableName = VariableName.TrimStart('.');
            else if (VariableName.IndexOf("I_") != -1) VariableName = VariableName.TrimStart('.');
            else if (VariableName.IndexOf("Main_Sys.JOGSetVelo") != -1) VariableName = VariableName.TrimStart('.');
            else VariableName = "_03_HMI" + VariableName;
            if (Beckhoff_PLC.WriteVariable(VariableName, ushort.Parse(val.Trim())))//将每个变量的变量名传来,在Tag中
            {
            }
            else
            {
                //MessageBox.Show("修改失败！", "修改提示");
            }
            Beckhoff_PLC.IsWriting = false;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否下发参数", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {

            }
            else
            {
                foreach (TabPage page in tabControl1.TabPages)
                {

        

                    foreach (Control item in page.Controls)
                    {
                        
                        if (item is TextBox)
                        {
                            if (item.Tag != null)
                            {
                                if (item.Tag.ToString().IndexOf("Main_Sys.Mach1Data[1]") != -1) //"Main_Sys.Mach1Data[1]"
                                {
                                    Beckhoff_PLC.IsWriting = true;
                                    Thread.Sleep(50);
                                    UpdateParameter_int(item.Tag.ToString().Trim(), item.Text.ToString().Trim());
                                    Beckhoff_PLC.IsWriting = false;
                                }
                                else if (item.Text != "" && item.Text != null && item.Text.IndexOf("PressConfirmed") == -1 && item.Text.IndexOf("StopTorqueConfirmed") == -1)  // && item.Text != "0" && item.Text != "0.0")
                                {
                                    Beckhoff_PLC.IsWriting = true;
                                    Thread.Sleep(50);
                                    UpdateParameter(item.Tag.ToString().Trim(), item.Text.ToString().Trim());
                                    Beckhoff_PLC.IsWriting = false;
                                }
                                else if (item.Text.IndexOf("PressConfirmed") != -1)
                                {
                                    Beckhoff_PLC.IsWriting = true;
                                    if (objSettings.PressConfirmed == false)
                                    {
                                        Beckhoff_PLC.WriteVariable(".PressConfirmed", true);
                                    }
                                    if (objSettings.PressConfirmed == true)
                                    {
                                        Beckhoff_PLC.WriteVariable(".PressConfirmed", false);
                                    }
                                    Beckhoff_PLC.IsWriting = false;

                                }
                                else if (item.Text.IndexOf("StopTorqueConfirmed") != -1)
                                {
                                    Beckhoff_PLC.IsWriting = true;
                                    if (objSettings.StopTorqueConfirmed == false)
                                    {
                                        Beckhoff_PLC.WriteVariable(".StopTorqueConfirmed", true);
                                    }
                                    if (objSettings.StopTorqueConfirmed == true)
                                    {
                                        Beckhoff_PLC.WriteVariable(".StopTorqueConfirmed", false);
                                    }
                                    Beckhoff_PLC.IsWriting = false;

                                }
                            }

                        }
                    } 
                }
                 
                for (int i = 0; i < dgv_data.Rows.Count; i++)
                { 
                    if (dgv_data.Rows[i].Selected == true)
                    {
                        CommonMethods.SettingsIndexBak = i + 1;
                    }
                } 
            }
        }

        private void Btn_Search_Click_1(object sender, EventArgs e)
        {
            UpdateDGV_ByName(txt_SearchName.Text);
            //var resultList
            //CommonMethods.LayerStackTmpList = CommonMethods.LayerStackList.Where(x => x.LayerStackName == "al_1.0+steel_2.0").ToList();  
        }

        private void MainPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tabPage5_Click(object sender, EventArgs e)
        {

        }

        private void txt_MultistageId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
                e.Handled = true;
        }

        private void txt_MultistageId_TextChanged(object sender, EventArgs e)
        {
            if (txt_MultistageId.Text == "")
                txt_MultistageId.Text = 1.ToString();
            int number = int.Parse(txt_MultistageId.Text);
            txt_MultistageId.Text = number.ToString();
            if (number >= 1 && number <= 5)
            {
                // return;
                txt_MultistageId.Text = number.ToString();
            }
            else if (number < 1)
            {
                txt_MultistageId.Text = "1";
                number = 1;
            }
            else if (number > 5)
            {
                txt_MultistageId.Text = "5";
                number = 5;
            }

            switch (number)
            {
                case 1:
                    txt_TargetPos2.Text = "0";
                    txt_TargetVelo2.Text = "0";
                    txt_RotationalSpeed2.Text = "0";
                    txt_TargetPos3.Text = "0";
                    txt_TargetVelo3.Text = "0";
                    txt_RotationalSpeed3.Text = "0";
                    txt_TargetPos4.Text = "0";
                    txt_TargetVelo4.Text = "0";
                    txt_RotationalSpeed4.Text = "0";
                    txt_TargetPos5.Text = "0";
                    txt_TargetVelo5.Text = "0";
                    txt_RotationalSpeed5.Text = "0";
                    break;
                case 2:
                    txt_TargetPos3.Text = "0";
                    txt_TargetVelo3.Text = "0";
                    txt_RotationalSpeed3.Text = "0";
                    txt_TargetPos4.Text = "0";
                    txt_TargetVelo4.Text = "0";
                    txt_RotationalSpeed4.Text = "0";
                    txt_TargetPos5.Text = "0";
                    txt_TargetVelo5.Text = "0";
                    txt_RotationalSpeed5.Text = "0";
                    break;
                case 3:
                    txt_TargetPos4.Text = "0";
                    txt_TargetVelo4.Text = "0";
                    txt_RotationalSpeed4.Text = "0";
                    txt_TargetPos5.Text = "0";
                    txt_TargetVelo5.Text = "0";
                    txt_RotationalSpeed5.Text = "0";
                    break;
                case 4:
                    txt_TargetPos5.Text = "0";
                    txt_TargetVelo5.Text = "0";
                    txt_RotationalSpeed5.Text = "0";
                    break;
                case 5:

                    break;
                default:
                    break;
            }
        }

        private void btn_PressConfirmed_Click(object sender, EventArgs e)
        { 
            //string str = "Update ";
            if (this.btn_PressConfirmed.BackColor == Color.FromArgb(60, 179, 113))
            { 
                this.btn_PressConfirmed.BackColor = Color.FromArgb(255, 255, 255); 
            }
            else
            { 
                this.btn_PressConfirmed.BackColor = Color.FromArgb(60, 179, 113); 
            }
             
            //Beckhoff_PLC.IsWriting = true;
            //if (CommonMethods.CurrentValue["PressConfirmed"] == "False")
            //{
            //    Beckhoff_PLC.WriteVariable(".PressConfirmed", true);
            //}
            //if (CommonMethods.CurrentValue["PressConfirmed"] == "True")
            //{
            //    Beckhoff_PLC.WriteVariable(".PressConfirmed", false);
            //}
            //Beckhoff_PLC.IsWriting = false;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.btn_StopTorqueConfirmed.BackColor == Color.FromArgb(60, 179, 113))
            {
                this.btn_StopTorqueConfirmed.BackColor = Color.FromArgb(255, 255, 255);
            }
            else
            { 
                this.btn_StopTorqueConfirmed.BackColor = Color.FromArgb(60, 179, 113); 
            }

            //Beckhoff_PLC.IsWriting = true;
            //if (CommonMethods.CurrentValue["StopTorqueConfirmed"] == "False")
            //{
            //    Beckhoff_PLC.WriteVariable(".StopTorqueConfirmed", true);
            //}
            //if (CommonMethods.CurrentValue["StopTorqueConfirmed"] == "True")
            //{
            //    Beckhoff_PLC.WriteVariable(".StopTorqueConfirmed", false);
            //}
            //Beckhoff_PLC.IsWriting = false;

        }

        private void cbo_Fastener_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                CommonMethods.FastenerTmpList = CommonMethods.FastenerList.Where(x => x.FastenerName == cbo_Fastener.SelectedValue).ToList();
                //this.txt_FastenerId.Text = CommonMethods.FastenerTmpList[0].FastenerId.ToString().PadLeft(2, '0');
                this.txt_FastenerName.Text = CommonMethods.FastenerTmpList[0].FastenerName;
                this.txt_Para_D.Text = CommonMethods.FastenerTmpList[0].Para_D.ToString();
                this.txt_Para_P.Text = CommonMethods.FastenerTmpList[0].Para_P.ToString();
                this.txt_Para_H.Text = CommonMethods.FastenerTmpList[0].Para_H.ToString();
                this.txt_Para_L.Text = CommonMethods.FastenerTmpList[0].Para_L.ToString();
            }
            catch
            {

            }


        }

        private void cbo_LayerStack_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                CommonMethods.LayerStackTmpList = CommonMethods.LayerStackList.Where(x => x.LayerStackName == cbo_LayerStack.SelectedValue).ToList();
                this.txt_LayerStackName.Text = CommonMethods.LayerStackTmpList[0].LayerStackName;
                this.txt_Material1.Text = CommonMethods.LayerStackTmpList[0].Material1;
                this.txt_Thickness1.Text = CommonMethods.LayerStackTmpList[0].Thickness1.ToString();
                this.txt_Material2.Text = CommonMethods.LayerStackTmpList[0].Material2;
                this.txt_Thickness2.Text = CommonMethods.LayerStackTmpList[0].Thickness2.ToString();
            }
            catch
            {

            }
        }
    }
}
